﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;

namespace ProjectManageSystemApi.Controllers
{
    /// <summary>
    /// 人员接口
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    [Authorize]
    public class UserController : Controller
    {
        readonly IUserServices _user;
        readonly IUserPasswordServices _userPwd;
        public UserController(IUserServices user, IUserPasswordServices userPwd) 
        {
            _user = user;
            _userPwd = userPwd;
        }

        /// <summary>
        /// 创建用户信息
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost,Route("Create")]
        [Authorize]
        public ApiResult Create([FromBody] UserInfoDto userInfo) 
        {
            if (userInfo == null || userInfo.UserName.IsNullOrEmpty() || userInfo.Password.IsNullOrEmpty()) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (_user.ExistByUserName(userInfo.UserName)) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.ACCOUNT_NAME_EXIST);
            }
            if (!_user.Insert(userInfo)) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }

        /// <summary>
        /// 修改用户信息，密码不允许修改
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost, Route("Update")]
        [Authorize]
        public ApiResult Update([FromBody] UserInfoDto userInfo)
        {
            if (userInfo == null || userInfo.Id == 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_user.ExistByUserId(userInfo.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            if (!_user.Update(userInfo))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        [HttpPost, Route("UpdatePassword")]
        public ApiResult UpdatePassword([FromBody] UserPasswordDto userPassword) 
        {
            if (userPassword == null || userPassword.UserId == 0) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_user.ExistByUserId(userPassword.UserId)) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            return ApiResultStatus.Create(_userPwd.Update(userPassword.UserId, userPassword.Password), ApiResultCodes.OPERATION_FAILED);
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("GetAllUser")]
        [Authorize]
        public ApiResult GetAllUser(PageBo<UserParam> pageBo)
        {
            if (pageBo == null)
            {
                pageBo = new PageBo<UserParam>();
            }
            if (pageBo.Param == null) 
            {
                pageBo.Param = new UserParam();
            }
            var data = _user.GetAll(pageBo);
            return ApiResultStatus.Success(data);
        }
    }
}
